package com.cs.study.homework._0308;

public class Demo {
    public static final Object OBJECT = new Object();

    public static void main(String[] args) {
        // 创建A线程 1 3 5....99

        new Thread(() -> {
            // sync
            synchronized (OBJECT) {
                for (int i = 1; i < 100; i += 2) {
                    // 唤醒另一个线程

                    OBJECT.notify();
                    // 打印
                    System.out.println(Thread.currentThread().getName() +
                            "----" + i);
                    // 阻止自己打印 wait

                    try {
                        OBJECT.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    // notify 把对方唤醒
                    OBJECT.notify();

                }
            }
        }, "A").start();


         //sleep
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 创建B线程 2 4 6.....100
        new Thread(() -> {
            // sync
            synchronized (OBJECT) {
                for (int i = 2; i <= 100; i += 2) {

                    // 唤醒另一个线程
                    OBJECT.notify();
                    // 打印
                    System.out.println(Thread.currentThread().getName() +
                            "----" + i);
                    // 阻止自己打印 wait
                    try {
                        OBJECT.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // notify()唤醒对象
                    OBJECT.notify();
                }
            }
        }, "B").start();
    }
}
